﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla;
using Csla.Data;

namespace Kiosk.Library
{
    public partial class NotificationReadOnlyList
    {
        /// <summary>
        /// Returns all the Notifications that are events
        /// </summary>
	    public static NotificationReadOnlyList GetAllActiveEvents()
		{
            return DataPortal.Fetch<NotificationReadOnlyList>(new SingleCriteria<NotificationReadOnlyList, string>("Event"));
		}

        public static NotificationReadOnlyList GetAllActiveMeetings()
        {
            return DataPortal.Fetch<NotificationReadOnlyList>(new SingleCriteria<NotificationReadOnlyList, string>("Meeting"));
        }

        public static NotificationReadOnlyList GetAllActiveDeliveries()
        {
            return DataPortal.Fetch<NotificationReadOnlyList>(new SingleCriteria<NotificationReadOnlyList, string>("Delivery"));
        }	

		private void DataPortal_Fetch(SingleCriteria<NotificationReadOnlyList, string> criteria)
		{
            FetchAllActiveByTypeName(criteria.Value);
		}

		private void FetchAllActiveByTypeName(string typeName)
		{
		  RaiseListChangedEvents = false;
		  using (var ctx = ContextManager<Kiosk.Data.KioskDataContext>.GetManager(Database.ConnectionString))
		  {
			var data = from o in ctx.DataContext.Notification
                       where o.NotificationType.Name == typeName
                       && o.NotificationDate > DateTime.Now.AddMinutes(-30)
                       && o.NotificationDate < DateTime.Now.AddHours(4)
                       select o;
			IsReadOnly = false;
			foreach (var d in data)
			  this.Add(new NotificationInfo(d.NotificationID, d.NotificationTypeID, d.LocationID, d.EmployeeID, d.NotificationDate, d.Title, d.Description));
			IsReadOnly = true;
		  }
		  RaiseListChangedEvents = true;
		}
    }
}
